"""获取合约相关信息
同步获取，跟 hqthread 并行， 此处手动处理的， hqthread 作为自动化运行的时候执行的

"""
from common.helper_log import LogHelper as helper_log
import common.constants as const
from services.factory_hq import HQFactory
import pandas as pd
import datetime as dt

def get_all_contracts():
    for exchange in const.EXCHANGE_LIST:
        for symbol in const.SYMBOL_LIST:
            contract = f'{symbol}.{exchange}'

def get_kline_sa():
    """ 
    1. 获取 SA 的K线信息（如果没有进度信息，tonight 则从 20200101 开始； 如果有进度信息，从进度信息的 enddate 开始）
    2. 保存到本地文件
    3. 保存进度到本地文件
    """

    try:
        hqf = HQFactory()

        exchange ='ZCE'
        symbol = 'SA'
        str_today = dt.datetime.today().strftime("%Y%m%d")
        startdate = '20200101'

        contract = f'{symbol}.{exchange}'
        dfk = hqf.get_kline_day(contract, start_date=startdate, end_date=str_today)
        
        hqf.save(dfk)

        dfprogress = pd.DataFrame({"getdate": [str_today],
                   'code': f'{symbol}.{exchange}',
                   'startdate': [startdate],
                   'enddate': [str_today]})

        hqf.save_progress(dfprogress)

        helper_log.log_info(f'get_sa success:{contract}')

    except Exception as e:
        helper_log.log_error(f'get_sa error:{e}')


def get_kline_tradeday():
    """ 
     获取交易日的K线信息（如果没有进度信息，tonight 则从 20200101 开始； 如果有进度信息，从进度信息的 enddate 开始）
     保存到本地文件
    """

    try:
        hqf = HQFactory()

        str_today = dt.datetime.today().strftime("%Y%m%d")

        dfk = hqf.get_kline_day()
        
        hqf.save(dfk)

        dfprogress = pd.DataFrame({"getdate": [str_today],
                   'code': f'{symbol}.{exchange}',
                   'startdate': [startdate],
                   'enddate': [str_today]})

        hqf.save_progress(dfprogress)

        helper_log.log_info(f'get_sa success:{contract}')

    except Exception as e:
        helper_log.log_error(f'get_sa error:{e}')


def get_contracts(exchange, symbol):
    """
    获取合约信息
    """
    try:
        contract = f'{symbol}.{exchange}'   

    except Exception as e: 
        helper_log.log_error(f'get_contracts error:{e}')
